package com.gaogzhen.designPattern.guardedSuspension;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

/**
 * 有时限等待测试
 */
@Slf4j(topic = "d.GuardedTest02")
public class GuardedTest02 {
    public static void main(String[] args) {
        // 线程通信对象
        GuardedTimeout object = new GuardedTimeout();
        // 线程1需要等待线程2下载完毕
        new Thread(() -> {
            // 阻塞式等待结果
            Object response = object.get(2000);
            log.debug("返回的是：{}", response);
        }, "t1").start();

        // 线程2执行下载任务
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            object.set(new Object());
        }, "t2").start();
    }
}
